﻿using INTEX.DataBase.Tools;
using System;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using WPFExcelReport;
using INTEX.DataBase.Tools;
namespace Intex.Tools
{
    public partial class Convert2Intex : BaseFunction
    {
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        } 
        public DataSet SetExt;
        public DataRow dwIntexStd = null;
        public string ITXDBVERSION = "50.24";
        public Convert2Intex()
        {
        }
        public Convert2Intex(DataSet set, DataRow row, OracleConnection _OracleDB, IntexReptServBLClient _proxy)
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet(); 
            SetExt = set;
            Result = Intex_MAP();
            SetExt.Merge(Result);
            dwIntexStd = SetExt.Tables["INTEXMAP"].Rows[0];
        }
        public DataSet Intex_MAP()
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "INTEX_MAP";
            DataTable tbMapID = new DataTable();

            tbMapID = SetExt.Tables[mapID];
            string FIELD = "";
            DataTable tbIntex = new DataTable();//dwIntexStd.Table.Clone();
            tbIntex.TableName = "INTEXMAP";
            DataRow dwIntex = tbIntex.NewRow();
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim() == "INTEXMAP"
                        orderby System.Convert.ToInt32(dwMap["ROWID"].ToString())
                        select dwMap;
            foreach (DataRow dwTemp in query)
            {
                GetCovnertValue(ref dwIntex, dwTemp, dwIntex);
                dwIntexStd = dwIntex;
            }
            
            ITXDBVERSION = "50.24";
            if (dwIntex.Table.Columns.Contains("ITXDBVERSION"))
            {
                ITXDBVERSION = dwIntex["ITXDBVERSION"].ToString();
            }
            tbIntex.Rows.Add(dwIntex);
            setReturn.Merge(tbIntex);
            return setReturn;
        }
        /// <summary>
        /// 数据来源有几种可能
        /// 1.外部的表。如果dwCAD不存在数据，则获得CAD的对应表的第一行数据。有时候，是CAD表的多行数据轮换
        /// 2.中间表。如果是中间表，由转换函数自己决定。
        /// </summary>
        /// <param name="dwIntex"></param>
        /// <param name="dwRule"></param>
        /// <param name="dwCAD"></param>
        /// <returns></returns>
        public object GetCovnertValue(ref DataRow dwIntex, DataRow dwRule, DataRow dwCAD = null)
        {
            string conv = dwRule["CONVERTER"].ToString().Trim();
            string value1, value2;
            int itxFieldLen;
            string cadfield = "";
            string cadTable = "";
            string itxtable = "";
            string itxfield = "", itxFieldtype = "";

            object result = "";
            string tbData = "";
            bool isDefault = false;
            int part = 1;
            //CADFILE	CADFIELD	CADDATATYPE	CADDATALENGTH	CONVERTER
            itxfield = dwRule["FIELD"].ToString().Trim();
            cadfield = dwRule["CADFIELD"].ToString().Trim();
            cadTable = dwRule["CADFILE"].ToString().Trim();
            if (dwCAD == null)
            {
                dwCAD = GetDataRow(dwCAD, "", cadTable);
            }
            if (dwCAD != null)
            {
                tbData = dwCAD.Table.TableName;
            }
            //if (dwCAD == null)
            //return;
            itxFieldLen = System.Convert.ToInt16(dwRule["DATALENGTH"].ToString());
            itxFieldtype = dwRule["DATATYE"].ToString();
            if (itxFieldtype == "NUMBER")
            {
                result = "0";
            }
            part = System.Convert.ToInt16(dwRule["PART"].ToString());
            if (itxfield == "VKKDECKPROZ")
            {
                Console.Write("VKKDECKPROZ");
            }
            switch (conv)
            {
                case "DEFAULT":
                    if (cadTable == "INTEXDEFAULT")
                    {
                        isDefault = true;
                        result = dwRule["DEFAULT"].ToString().Replace("@", " ");
                    }
                    result = MeetYourLength((string)result, itxFieldLen, itxFieldtype);

                    break;
                #region
                case "XYEAR":
                    result = XYEAR();
                    break;
                case "XMONTH":
                    result = XMONTH();
                    break;

                case "LGUID":
                    result = LGUID();
                    break;
                case "ITXKUNDNR":
                    result = MeetYourLength(dwIntexStd["ITXKUNDNR"].ToString(), itxFieldLen, itxFieldtype);
                    break;
                case "ITXKUNDNAME":
                    result = MeetYourLength(dwIntexStd["ITXKUNDNAME"].ToString(), itxFieldLen, itxFieldtype);
                    break;
                case "ITXLOOMTYPE":
                    result = MeetYourLength(dwIntexStd["ITXLOOMTYPE"].ToString(), itxFieldLen, itxFieldtype);
                    break;
                case "ITXVKKRS":
                    result = MeetYourLength(dwIntexStd["ITXVKKRS"].ToString(), itxFieldLen, itxFieldtype);
                    break;
                case "ITXVKLFD":
                    result = MeetYourLength(dwIntexStd["ITXVKLFD"].ToString(), itxFieldLen, itxFieldtype);
                    break;
                case "ITXMANDANT":
                    // ITXMANDANT ITXART ITXUSER ITXDATE
                    result = MeetYourLength(dwIntexStd["ITXMANDANT"].ToString(), itxFieldLen, itxFieldtype);
                    break;
                case "ITXCADID":
                    result = MeetYourLength(dwIntexStd["ITXCADID"].ToString(), itxFieldLen, itxFieldtype);
                    break;
                case "ITXFART":
                    result = MeetYourLength(dwIntexStd["ITXFART"].ToString(), itxFieldLen, itxFieldtype);
                    break;
                case "ITXFVAR":
                    result = MeetYourLength(dwIntexStd["ITXFVAR"].ToString(), itxFieldLen, itxFieldtype);
                    break;
                case "ITXFFINISH":
                    result = MeetYourLength(dwIntexStd["ITXFFINISH"].ToString(), itxFieldLen, itxFieldtype);
                    break;
                case "ITXFCOLOR":
                    result = MeetYourLength(dwIntexStd["ITXFCOLOR"].ToString(), itxFieldLen, itxFieldtype);
                    break;
                case "ITXGART":
                    result = MeetYourLength(dwIntexStd["ITXGART"].ToString(), itxFieldLen, itxFieldtype);
                    break;
                case "ITXGVAR":
                    result = MeetYourLength(dwIntexStd["ITXGVAR"].ToString(), itxFieldLen, itxFieldtype);
                    break;
                case "ITXGCOLOR":
                    result = MeetYourLength(dwIntexStd["ITXGCOLOR"].ToString(), itxFieldLen, itxFieldtype);
                    break;
                case "ITXBART":
                    result = MeetYourLength(dwIntexStd["ITXBART"].ToString(), itxFieldLen, itxFieldtype);
                    break;
                case "ITXBVAR":
                    result = MeetYourLength(dwIntexStd["ITXBVAR"].ToString(), itxFieldLen, itxFieldtype);
                    break;
                case "ITXBCOLOR":
                    result = MeetYourLength(dwIntexStd["ITXBCOLOR"].ToString(), itxFieldLen, itxFieldtype);
                    break;
                case "ITXUSER":
                    result = MeetYourLength(dwIntexStd["ITXUSER"].ToString(), itxFieldLen, itxFieldtype);
                    break;
                case "ITXDATE":
                    result = dwIntexStd["ITXDATE"].ToString();
                    break;
                case "ITXFLEN":
                    result = dwIntexStd["ITXFLEN"].ToString();
                    break;
                case "ITXFUNIT":
                    result = dwIntexStd["ITXFUNIT"].ToString();
                    break;
                case "ITXGLEN":
                    result = dwIntexStd["ITXGLEN"].ToString();
                    break;
                case "ITXBLEN":
                    result = dwIntexStd["ITXBLEN"].ToString();
                    break;
                case "ITXFGRATIO":
                    result = dwIntexStd["ITXFGRATIO"].ToString();
                    break;
                case "ITXGBRATIO":
                    result = dwIntexStd["ITXGBRATIO"].ToString();
                    break;
                #endregion
                case "BYTE":
                    if (cadTable == "INTEXDEFAULT")
                    {
                        isDefault = true;
                        result = System.Text.Encoding.ASCII.GetBytes(dwRule["DEFAULT"].ToString());
                    }
                    break;
                case "LBYTE":
                    result = LBYTE(dwCAD, dwRule, tbData, cadTable);
                    break;
                case "LESSFIX":
                    result = LESSFIX(dwCAD, dwRule, tbData, cadTable);
                    break;
                case "SDKEY1":
                    tbData = dwIntex.Table.TableName;
                    result = SDKEY1(dwIntex, dwRule, tbData, cadTable);
                    break;
                case "LWEIGHT":
                    tbData = dwIntex.Table.TableName;
                    result = LWEIGHT(dwIntex, dwRule, tbData, cadTable);
                    break;
                case "EQ":
                    tbData = dwIntex.Table.TableName;
                    result = EQ(dwIntex, dwRule, tbData, cadTable);
                    break;
                case "LWBPOS":
                    tbData = dwCAD.Table.TableName;
                    result = LWBPOS(dwCAD, dwRule, tbData, cadTable);
                    break;
                case "LBELEG":
                    result = LBELEG(dwCAD, dwRule, tbData, cadTable);
                    break;
                case "LSTKNR":
                    result = LSTKNR(dwCAD, dwRule, tbData, cadTable);
                    break;
                case "ROWID1":
                    result = ROWID1(dwCAD, dwRule, tbData, cadTable);
                    break;
                case "NROWID":
                    result = NROWID(dwCAD, dwRule, tbData, cadTable);
                    break;
                case "NROWID2":
                    result = NROWID2(dwCAD, dwRule, tbData, cadTable);
                    break;
                case "NROWID3":
                    result = NROWID3(dwCAD, dwRule, tbData, cadTable);
                    break;
                case "JKD03":
                    result = JKD03(dwCAD, dwRule, tbData, cadTable);
                    break;
                case "TIMES":
                    result = TIMES(dwCAD, dwRule, tbData, cadTable);
                    break;
                case "YTIMES":
                    tbData = dwIntex.Table.TableName;
                    result = YTIMES(dwIntex, dwRule, tbData, cadTable);
                    break;
                case "TIMES2":
                    result = TIMES2(dwCAD, dwRule, tbData, cadTable);
                    break;
                case "TIMES21":
                    result = TIMES21(dwCAD, dwRule, tbData, cadTable);
                    break;
                case "TIMES3":
                    tbData = dwIntex.Table.TableName;
                    result = TIMES3(dwIntex, dwRule, tbData, cadTable);
                    break;
                case "ROWCOUNT":
                    result = ROWCOUNT(dwCAD, dwRule, tbData, cadTable);
                    break;
                case "TRATIO":
                    tbData = dwIntex.Table.TableName;
                    result = TRATIO(dwIntex, dwRule, tbData, cadTable);
                    break;
                case "JRATIO":
                    result = JRATIO(dwIntex, dwRule, tbData, cadTable);
                    break;
                case "COMBINE2":
                    result = COMBINE2(dwCAD, dwRule, tbData, cadTable);
                    result = ConvertGB2312To8859P1((string)result);
                    result = MeetYourLength((string)result, itxFieldLen, itxFieldtype);
                    break;
                case "COMBINE4":
                    result = COMBINE4(dwIntex, dwRule, tbData, cadTable);
                    break;
                case "HRSTAMM":
                    result = GetHRSTAMM(dwCAD, dwRule, tbData, cadTable);
                    break;
                case "DATEPROCESS1":
                    result = DATEPROCESS1(dwCAD, dwRule, tbData, cadTable);
                    break;
                case "LEFT":
                    result = LEFT(dwCAD, dwRule, tbData, cadTable);
                    result = MeetYourLength((string)result, itxFieldLen, itxFieldtype);
                    break;
                case "LNUM":
                    result = LNUM(dwCAD, dwRule, tbData, cadTable);
                    result = MeetYourLength((string)result, itxFieldLen, itxFieldtype);
                    break;

                case "LOOKUP":
                    result = LOOKUP(dwCAD, dwRule, tbData, cadTable);
                    result = MeetYourLength((string)result, itxFieldLen, itxFieldtype);
                    break;
                case "NZERO":
                    result = NZERO(dwCAD, dwRule, tbData, cadTable);
                    break;
                case "REPLACE":
                    result = REPLACE(dwCAD, dwRule, tbData, cadTable);
                    break;
                case "DLEFT":
                    result = LEFT(dwCAD, dwRule, tbData, cadTable);
                    result = ConvertGB2312To8859P1((string)result);
                    result = MeetYourLength((string)result, itxFieldLen, itxFieldtype);
                    break;
                default:
                    break;
            }
            if (dwIntex != null)
            {
                if (part > 1)
                {
                    string spart = dwIntex[itxfield].ToString();
                    result = spart + result;
                }
                if (!dwIntex.Table.Columns.Contains(itxfield))
                {
                    switch (itxFieldtype)
                    {
                        case "RAW":
                            dwIntex.Table.Columns.Add(itxfield, typeof(Byte[]));
                            break;
                        case "NUMBER":
                            dwIntex.Table.Columns.Add(itxfield, typeof(decimal));
                            break;
                        default:
                            dwIntex.Table.Columns.Add(itxfield);
                            break;
                    }
                }
                dwIntex[itxfield] = result;
            }
            return result;
        }

        #region======Converter==========
        public string COMBINE2(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = "";
            string value1, value2, value3, value4, value5;
            string seperator = "";
            int itxFieldLen = 1;

            dwData = GetDataRow(dwData, tbData, exttb);
            value1 = dwRule["VALUE1"].ToString();
            value2 = dwRule["VALUE2"].ToString();
            value3 = dwRule["VALUE3"].ToString();
            value4 = dwRule["VALUE4"].ToString();
            itxFieldLen = System.Convert.ToInt16(value3);
            seperator = value4.Trim();
            result = dwData[value1].ToString().Trim() + seperator + dwData[value2].ToString().Trim() + "                                                                               ";
            if (result.Length > itxFieldLen)
                result = Left(result, itxFieldLen);
            return result;
        }
        public string COMBINE3(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = "";
            string value1, value2, value3, value4, value5;
            string seperator = "";
            int itxFieldLen = 1;
            //通过品名+色号，确定一个色号
            //品名用INTEXMAP表中的ITXMAINID字段
            DataRow row1, row2;
            value1 = dwRule["VALUE1"].ToString().Trim();//table1
            value2 = dwRule["VALUE2"].ToString().Trim();//field1
            value3 = dwRule["VALUE3"].ToString().Trim();//table2
            value4 = dwRule["VALUE4"].ToString().Trim();//field2
            value5 = dwRule["VALUE5"].ToString().Trim();//length

            row1 = GetDataRow(dwData, tbData, value1);
            row2 = GetDataRow(dwData, tbData, value3);
            itxFieldLen = System.Convert.ToInt16(value5);

            result = row1[value2].ToString().Trim() + row2[value4].ToString().Trim() + "                                                                               ";
            if (result.Length > itxFieldLen)
                result = Left(result, itxFieldLen);
            return result;
        }
        public string COMBINE4(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = "";
            string value1, value2, value3, value4, value5;
            string seperator = "";
            int itxFieldLen = 1;
            dwData = GetDataRow(dwData, tbData, exttb);
            value1 = dwRule["VALUE1"].ToString();
            value2 = dwRule["VALUE2"].ToString();
            value3 = dwRule["VALUE3"].ToString();
            itxFieldLen = System.Convert.ToInt16(value3);
            result = dwData[value1].ToString().Trim() + dwData[value2].ToString().Trim() + "                                                                               ";
            if (result.Length > itxFieldLen)
                result = Left(result, itxFieldLen);
            return result;
        }
        public string DATEPROCESS1(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = Today;
            string value1, value2;
            int itxFieldLen = 1;

            dwData = GetDataRow(dwData, tbData, exttb);
            value1 = dwRule["VALUE1"].ToString();
            //2012/2/7 T10:36:02.05+08:00
            string caddate = dwData[value1].ToString();
            int sintex = caddate.IndexOf("/");
            int lindex = caddate.LastIndexOf("/");
            int leng = caddate.Length;
            if (sintex > 0
                && lindex > 0
                )
            {
                result = caddate.Substring(0, sintex)
                     + Right("00" + caddate.Substring(sintex + 1, lindex - sintex - 1), 2)
                     + Right("00" + caddate.Substring(lindex + 1, 2).Trim(), 2);
            }
            else
            {
                result = Today;
            }
            /*
           result = caddate.Substring(lindex + 1, 4).Trim()
               + Right("00" + caddate.Substring(0, sintex), 2)
               + Right("00" + caddate.Substring(sintex + 1, lindex - sintex - 1), 2);*/
            if (result == "")
            {
                result = Today;
            }
            if (result.Contains(":") || result.Contains("/")
                || result.Length < 8
                )
            {
                result = DateTime.Now.Year.ToString() + Right("00" + DateTime.Now.Month.ToString(), 2) + Right("00" + DateTime.Now.Day.ToString(), 2);
            }
            return result;
        }

        public object LBYTE(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            object result = "";
            string value1;
            int itxFieldLen = 1;
            dwData = GetDataRow(dwData, tbData, exttb);
            value1 = dwRule["VALUE1"].ToString().Trim();
            itxFieldLen = System.Convert.ToInt16(dwRule["DATALENGTH"].ToString());
            result = System.Text.Encoding.ASCII.GetBytes(dwData[value1].ToString());
            return result;
        }
        public string LEFT(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = "";
            string value1, value2;
            int itxFieldLen = 1;
            string dflt = dwRule["DEFAULT"].ToString();
            try
            {
                dwData = GetDataRow(dwData, tbData, exttb);
            }
            catch (Exception ex)
            { 
                result = dflt.PadRight(100, ' ');
                if (result.Length > itxFieldLen)
                    result = Left(result, itxFieldLen);
                return result;
            }
            value1 = dwRule["VALUE1"].ToString().Trim();
            itxFieldLen = System.Convert.ToInt16(dwRule["DATALENGTH"].ToString());
            if (dwData.Table.Columns.Contains(value1))
            {
                result = dwData[value1].ToString().PadRight(100, ' ');
            }
            else
            {
                result = dflt.PadRight(100, ' ');
            }
            if (result.Length > itxFieldLen)
                result = Left(result, itxFieldLen);
            return result;
        }
        public string LNUM(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = "";
            string value1, value2;
            int itxFieldLen = 1;

            dwData = GetDataRow(dwData, tbData, exttb);
            value1 = dwRule["VALUE1"].ToString().Trim();
            itxFieldLen = System.Convert.ToInt16(dwRule["DATALENGTH"].ToString());
            result = "0000000000000000000000000" + dwData[value1].ToString().Trim();
            if (result.Length > itxFieldLen)
                result = Right(result, itxFieldLen);
            return result;
        }

        public string LOOKUP(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = "F99999";
            string value1, value2, value3, value4, value5;
            int itxFieldLen = 1;
            DataRow row1, row2;
            value1 = dwRule["VALUE1"].ToString().Trim();
            value2 = dwRule["VALUE2"].ToString().Trim();
            value3 = dwRule["VALUE3"].ToString().Trim();
            value4 = dwRule["VALUE4"].ToString().Trim();
            value5 = dwRule["VALUE5"].ToString().Trim();
            row1 = GetDataRow(dwData, tbData, value1);
            var query = from dwTemp in SetExt.Tables[value3].AsEnumerable()
                        where dwTemp[value5].ToString().Trim() == row1[value2].ToString().Trim()
                        select dwTemp;
            foreach (DataRow dwTemp in query)
            {
                result = dwTemp[value4].ToString();
            }
            itxFieldLen = System.Convert.ToInt16(dwRule["DATALENGTH"].ToString());
            result = result.PadRight(10, ' ');
            if (result.Length > itxFieldLen)
                result = Left(result, itxFieldLen);
            return result;
        }
        public string NZERO(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = "";
            string value1, value2, value3, value4;
            int itxFieldLen = 1;


            dwData = GetDataRow(dwData, tbData, exttb);
            value1 = dwRule["VALUE1"].ToString().Trim();
            value3 = dwRule["VALUE3"].ToString();
            value4 = "";
            itxFieldLen = System.Convert.ToInt16(dwRule["VALUE2"].ToString());
            result = dwData[value1].ToString();
            int first1 = result.IndexOfAny(new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9' });
            if (first1 > 0)
            {
                result = result.Substring(first1);
            }
            result = result.PadRight(100, ' ');
            if (result.Length > itxFieldLen)
                result = Left(result, itxFieldLen);
            result = result.Replace(value3, value4);
            return result;
        }
        public string REPLACE(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = "";
            string value1, value2, value3, value4;
            int itxFieldLen = 1;

            dwData = GetDataRow(dwData, tbData, exttb);
            value1 = dwRule["VALUE1"].ToString().Trim();
            value3 = dwRule["VALUE3"].ToString();
            value4 = "";
            itxFieldLen = System.Convert.ToInt16(dwRule["VALUE2"].ToString());
            result = dwData[value1].ToString().PadRight(100, ' ');
            if (result.Length > itxFieldLen)
                result = Left(result, itxFieldLen);
            result = result.Replace(value3, value4);
            return result;
        }
        //SELECT 10000*(1174/1300-1) FROM DUAL
        public string TRATIO(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = "0";
            double value1, value2;
            double factor = 1;

            value1 = System.Convert.ToDouble(dwData[dwRule["VALUE1"].ToString()].ToString());
            value2 = System.Convert.ToDouble(dwData[dwRule["VALUE2"].ToString()].ToString());
            factor = System.Convert.ToDouble(dwRule["VALUE3"].ToString());
            if (value2 == 0)
            {
                return result;
            }
            double dvalue = System.Convert.ToDouble(value1 / value2 - 1) * factor;
            result = System.Math.Round(dvalue, 0).ToString();
            return result;
        }
        public string JRATIO(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = "0";
            double value1, value2;
            double factor = 1;
            value1 = System.Convert.ToDouble(dwData[dwRule["VALUE1"].ToString()].ToString());
            value2 = System.Convert.ToDouble(dwData[dwRule["VALUE2"].ToString()].ToString());
            factor = System.Convert.ToDouble(dwRule["VALUE3"].ToString());
            if (value2 == 0)
            {
                return result;
            }
            double dvalue = System.Convert.ToDouble(value1 / value2) * factor;
            //result = System.Math.Round(dvalue, 0).ToString();
            result = dvalue.ToString();
            return result;
        }


        public string SDKEY1(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = "0";
            string value1, value2, value3, value4, value5;
            string sdsa = dwRule["VALUE2"].ToString();
            value3 = dwRule["VALUE3"].ToString();
            value4 = dwRule["VALUE4"].ToString();
            value5 = dwRule["VALUE5"].ToString();
            string term = value3;
            string krs = "";
            int target = System.Convert.ToInt16(value5);
            if (value4.Trim() != "")
            {
                krs = Left(dwData[value4].ToString(), 2);
            }
            if (krs == "**")
                return "";
            string sdkey = "00" + term + krs;
            sdkey = Left(sdkey + "                                           ", 22) + "00";
            string sddata =INTEXDBC.GetSDDAT(sdsa, sdkey);
            double lfd = System.Convert.ToDouble(sddata.Substring(47, 6));
            lfd++;
            result = Right("000000" + System.Math.Round(lfd, 0).ToString(), 6);
            sddata = Left(sddata, 47) + result + sddata.Substring(53);
            DataRow dwSDDAT = SetExt.Tables["SDDAT"].NewRow();
            dwSDDAT["SDSA"] = sdsa;
            dwSDDAT["SDKEY"] = sdkey;
            dwSDDAT["SDDATEN"] = sddata;
            INTEXDBC.UpdateSDDAT(dwSDDAT, ITXDBVERSION);
            result = Right("00000000000" + System.Math.Round(lfd, 0).ToString(), target);
            return result;
        }
        public string ROWID1(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = "0";
            string value1, value2, value3, value4;
            dwData = GetDataRow(dwData, tbData, exttb);
            value1 = dwRule["VALUE1"].ToString();
            string VKKAENDZL = dwData[value1].ToString();
            int pos = System.Convert.ToInt16(VKKAENDZL);
            pos++;
            VKKAENDZL = Right("000" + pos, 3);
            dwData[value1] = VKKAENDZL;
            result = VKKAENDZL;
            return result;
        }
        public string NROWID(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = "0";
            string value1, value2, value3, value4;
            dwData = GetDataRow(dwData, tbData, exttb);
            value1 = dwRule["VALUE1"].ToString();
            value2 = dwRule["VALUE2"].ToString();
            string apos = dwData[value1].ToString();
            int length = System.Convert.ToInt16(value2);
            apos = Right("000000" + apos, length);
            result = apos;
            return result;
        }
        public string NROWID3(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = "0";
            string value1, value2, value3, value4;
            double factor = 1;
            dwData = GetDataRow(dwData, tbData, exttb);
            value1 = dwRule["VALUE1"].ToString();
            factor = System.Convert.ToDouble(dwRule["VALUE2"].ToString());
            value3 = dwRule["VALUE3"].ToString();
            double dvalue = System.Convert.ToDouble(dwData[value1].ToString()) * factor;
            int length = System.Convert.ToInt16(value3);
            result = Right("000000" + dvalue, length);

            return result;
        }
        public string NROWID2(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = "0";
            string value1, value2, value3, value4;
            dwData = GetDataRow(dwData, tbData, exttb);
            value1 = dwRule["VALUE1"].ToString();
            value2 = dwRule["VALUE2"].ToString();
            string apos = dwData[value1].ToString();
            int length = System.Convert.ToInt16(value2);
            apos = Right("000" + apos, length);
            result = apos;
            return result;
        }
        public string TIMES(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = "";
            string value1, value2;
            double factor = 1;
            string dflt = dwRule["DEFAULT"].ToString();
            try
            {
                dwData = GetDataRow(dwData, tbData, exttb);
            }
            catch (Exception ex)
            {
                return dflt;
            } 
            value1 = dwRule["VALUE1"].ToString().Trim();
            if (value1 == "")
                return dflt;
            factor = System.Convert.ToDouble(dwRule["VALUE2"].ToString());
            string value = dwData[value1].ToString();
            if (value == "")
                value = dflt;
            double dvalue = System.Convert.ToDouble(value) * factor;
            result = System.Math.Round(dvalue, 0).ToString();
            return result;
        }
        public string YTIMES(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = "";
            string value1, value2, value3, value4, value5;
            double factor = 1;
            DataRow row1, row2;
            //dwData = GetDataRow(dwData, tbData, exttb);
            value1 = dwRule["VALUE1"].ToString().Trim();
            if (value1 == "")
                return "0";
            value2 = dwRule["VALUE2"].ToString().Trim();
            value3 = dwRule["VALUE3"].ToString().Trim();
            if (value3 == "")
                return "0";
            value4 = dwRule["VALUE4"].ToString().Trim();
            value5 = dwRule["VALUE5"].ToString().Trim();
            row1 = GetDataRow(dwData, tbData, value1);
            string value = row1[value2].ToString();
            if (value == "")
                value = "0";
            row2 = GetDataRow(dwData, tbData, value3);
            string ym = row2[value4].ToString();
            if (ym.Contains("Y"))
                factor = 0.9144 * System.Convert.ToDouble(value5);
            double dvalue = System.Convert.ToDouble(value) * factor;
            result = System.Math.Round(dvalue, 0).ToString();
            return result;
        }

        public string TIMES2(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = "";
            double factor = 1;
            string value1, value2, value3, value4, value5;
            int itxFieldLen = 1;
            DataRow row1, row2;
            value1 = dwRule["VALUE1"].ToString().Trim();
            if (value1 == "")
                return "0";
            value2 = dwRule["VALUE2"].ToString().Trim();
            value3 = dwRule["VALUE3"].ToString().Trim();
            if (value3 == "")
                return "0";
            value4 = dwRule["VALUE4"].ToString().Trim();
            row1 = GetDataRow(dwData, tbData, value1);
            string value = row1[value2].ToString();
            if (value == "")
                value = "0";

            row2 = GetDataRow(dwData, tbData, value3);

            factor = System.Convert.ToDouble(row2[value4].ToString());

            double dvalue = System.Convert.ToDouble(value) * factor;
            result = System.Math.Round(dvalue, 5).ToString();
            return result;
        }
        public string TIMES3(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = "0";
            double value1, value2;
            double factor = 1;
            factor = System.Convert.ToDouble(dwRule["VALUE1"].ToString());
            value2 = System.Convert.ToDouble(dwData[dwRule["VALUE2"].ToString()].ToString());
            if (value2 == 0)
            {
                return result;
            }
            double dvalue = System.Convert.ToDouble(factor / value2);
            result = dvalue.ToString();
            return result;
        }
        public string TIMES21(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = "";
            double factor = 1, factor2 = 1;
            string value1, value2, value3, value4, value5;
            int itxFieldLen = 1;
            DataRow row1, row2;
            value1 = dwRule["VALUE1"].ToString().Trim();
            if (value1 == "")
                return "0";
            value2 = dwRule["VALUE2"].ToString().Trim();
            value3 = dwRule["VALUE3"].ToString().Trim();
            if (value3 == "")
                return "0";
            value4 = dwRule["VALUE4"].ToString().Trim();
            value5 = dwRule["VALUE5"].ToString().Trim();

            row1 = GetDataRow(dwData, tbData, value1);
            string value = row1[value2].ToString();
            if (value == "")
                value = "0";

            row2 = GetDataRow(dwData, tbData, value3);

            factor = System.Convert.ToDouble(row2[value4].ToString());
            factor2 = System.Convert.ToDouble(value5);

            double dvalue = System.Convert.ToDouble(value) * factor * factor2;
            result = System.Math.Round(dvalue, 5).ToString();
            return result;
        }
        public string LWEIGHT(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = "";
            double factor = 1;
            string value1, value2, value3, value4, value5;
            int itxFieldLen = 1;
            DataRow row1, row2;
            value1 = dwRule["VALUE1"].ToString().Trim();
            if (value1 == "")
                return "0";
            value2 = dwRule["VALUE2"].ToString().Trim();
            value3 = dwRule["VALUE3"].ToString().Trim();
            if (value3 == "")
                return "0";
            value4 = dwRule["VALUE4"].ToString().Trim();
            value5 = dwRule["VALUE5"].ToString().Trim();
            row1 = GetDataRow(dwData, tbData, value1);
            string swight = row1[value2].ToString();
            if (swight == "")
                swight = "0";
            string slength = row1[value3].ToString();

            row2 = GetDataRow(dwData, tbData, value4);
            string tlength = row2[value5].ToString();

            double dvalue = System.Convert.ToDouble(tlength) / System.Convert.ToDouble(slength) * System.Convert.ToDouble(swight);
            result = System.Math.Round(dvalue, 0).ToString();
            return result;
        }
        public string EQ(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = "";
            double factor = 1;
            string value1, value2, value3, value4, value5;
            int itxFieldLen = 1;
            DataRow row1, row2;
            value1 = dwRule["VALUE1"].ToString().Trim();
            if (value1 == "")
                return "0";

            result = dwData[value1].ToString();
            return result;
        }

        public string LWBPOS(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = "";
            double factor = 1;
            string value1, value2, value3, value4, value5;
            int itxFieldLen = 1;
            DataRow row1, row2;
            value1 = dwRule["VALUE1"].ToString().Trim();
            if (value1 == "")
                return "0";
            value2 = dwRule["VALUE2"].ToString().Trim();
            value3 = dwRule["VALUE3"].ToString().Trim();
            if (value3 == "")
                return "0";
            row1 = GetDataRow(dwData, tbData, value1);
            double pos = System.Convert.ToDouble(row1[value2].ToString()) + 1;
            string spos = Right("0000" + System.Math.Round(pos, 0), 4);
            row1[value2] = spos;
            if (tbData == value1)
            {
                dwData[value2] = spos;
            }
            return spos;
        }
        public string LBELEG(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = "";
            DataSet Result = INTEXDBC.QueryWBBEW(null, 1);
            string BELEG = "";
            BELEG = Result.Tables[0].Rows[0][0].ToString();
            BELEG = System.Math.Round(System.Convert.ToDouble(BELEG) + 1, 0).ToString();
            return BELEG;
        }
        public string LSTKNR(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = "";
            double factor = 1;
            string value1, value2, value3, value4, value5;
            int itxFieldLen = 1;
            DataRow row1, row2;
            value1 = dwRule["VALUE1"].ToString().Trim();
            if (value1 == "")
                return "0";
            value2 = dwRule["VALUE2"].ToString().Trim();
            value3 = dwRule["VALUE3"].ToString().Trim();
            if (value3 == "")
                return "0";
            value4 = dwRule["VALUE4"].ToString().Trim();
            itxFieldLen = System.Convert.ToInt32(value4);
            row1 = GetDataRow(dwData, tbData, value1);
            string sstknr = row1[value2].ToString().Trim();
            string spos = row1[value3].ToString();
            spos = (System.Convert.ToDouble(spos) + 1).ToString();
            row1[value3] = spos;
            if (tbData == value1)
            {
                dwData[value3] = spos;
            }
            sstknr = Right(sstknr + "-" + spos, itxFieldLen);
            sstknr = Left(sstknr + "                  ", itxFieldLen);
            return sstknr;
        }
        public string LESSFIX(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = "";
            string value1, value2;
            int factor = 1;

            dwData = GetDataRow(dwData, tbData, exttb);
            value1 = dwRule["VALUE1"].ToString().Trim();
            if (value1 == "")
                return "0";
            factor = System.Convert.ToInt16(dwRule["VALUE2"].ToString());
            double dvalue = System.Convert.ToDouble(dwData[value1].ToString()) - factor;
            if (dvalue < 0)
                dvalue = 0;
            result = System.Convert.ToInt64(dvalue).ToString();

            return result;
        }
        public string XYEAR()
        {
            string result = "";
            result = System.DateTime.Now.Year.ToString();
            return result;
        }
        public string XMONTH()
        {
            string result = "";
            result = Right("00" + System.DateTime.Now.Month.ToString(), 2);
            return result;
        }

        public string LGUID()
        {
            string result = "";
            result = Guid.NewGuid().ToString();
            return result;
        }
        #endregion

        #region======Helper============
        public string Today
        {
            get
            {
                DateTime now = DateTime.Now;
                return now.Year.ToString() + Right("00" + now.Month.ToString(), 2) + Right("00" + now.Day.ToString(), 2);
            }
        }
        DataRow GetDataRow(DataRow dwData, string tbData, string exttb)
        {
            DataRow result = null;
            if (exttb == "INTEXDEFAULT")
                return dwData;
            if (exttb == "SDDAT")
                return dwData;
            if (tbData != exttb)
            {
                if (SetExt.Tables.Contains(exttb))
                {
                    if (SetExt.Tables[exttb].Rows.Count > 0)
                    {
                        result = SetExt.Tables[exttb].Rows[0];
                    }
                }
            }
            else
            {
                result = dwData;
            }
            if (result == null)
            {
                throw new Exception(exttb + "不存在");
            }
            return result;
        }
        public string Byte2String(byte[] arrByte)
        {

            StringBuilder sb = new StringBuilder();
            foreach (byte b in arrByte)
            {
                sb.Append(b > 15 ? Convert.ToString(b, 16) : '0' + Convert.ToString(b, 16));
            }
            return sb.ToString();
        }
        public string MeetYourLength(string input, int length, string dtype)
        {
            string result = input;
            if (dtype == "NUMBER")
            {
                result = input.Replace(" ", "");
            }
            else
            {
                result = Left(input.PadRight(length, ' '), length);
            }
            return result;
        }
        public string GetHRSTAMM(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = "CAD";
            string value1, value2;
            int itxFieldLen = 1;
            //HRBENID,HRVNAME,HRNNAME
            dwData = GetDataRow(dwData, tbData, exttb);
            if (dwData == null)
            {
                throw new Exception(exttb + "不存在");
            }
            value1 = dwRule["VALUE1"].ToString();
            value2 = dwRule["VALUE2"].ToString();

            string HRNAME = dwData[value1].ToString().Trim();
            OracleCommand or1 =app.OracleDB.CreateCommand();
            or1.CommandText = "SELECT * FROM HRSTAMM WHERE Trim(HRVNAME)||Trim(HRNNAME)=:HRNAME";
            or1.Parameters.Add("HRNAME", OracleType.NVarChar).Value = HRNAME;
            setResult = INTEXDBC.Connect2Proxy(or1);
            if (setResult.Tables.Count > 0)
            {
                if (setResult.Tables[0].Rows.Count > 0)
                {
                    result = setResult.Tables[0].Rows[0]["HRBENID"].ToString();
                }
            }

            return result;
        }
        public string ROWCOUNT(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = "CAD";
            string value1, value2;
            int itxFieldLen = 1;
            //HRBENID,HRVNAME,HRNNAME

            string cadTable = dwRule["CADFILE"].ToString();
            DataTable tbExt = SetExt.Tables[cadTable];
            value1 = dwRule["VALUE1"].ToString();
            value2 = dwRule["VALUE2"].ToString();
            var query = from dwExt in tbExt.AsEnumerable()
                        where dwExt[value1].ToString() == value2
                        select dwExt;
            result = query.CopyToDataTable().Rows.Count.ToString();
            return result;
        }
        public string JKD03(DataRow dwData, DataRow dwRule, string tbData, string exttb)
        {
            string result = "0";
            string value1, value2, value3, value4;
            DataSet setResult;
            dwData = GetDataRow(dwData, tbData, exttb);
            value1 = dwRule["VALUE1"].ToString();
            string KUNDNR = dwData[value1].ToString().Trim();
            string KUNDNAME = dwIntexStd["ITXKUNDNAME"].ToString();
            if (KUNDNR == "")
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT MAX(KDKUNDNR) AS KDKUNDNR FROM KDSTAMM WHERE KDMANDANT='00'";
                setResult = INTEXDBC.Connect2Proxy(or1);
                KUNDNR = setResult.Tables[0].Rows[0]["KDKUNDNR"].ToString();
                long kdnr = System.Convert.ToInt64(KUNDNR) + 1;
                KUNDNR = Right("00000000" + kdnr, 8);
                KUNDNR = "$" + KUNDNR; ;
            }
            result = KUNDNR;
            return result;
        }
        #endregion

    }
}
